package com.lib.krismile.base.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

/*
 * @author ： wangJiaWei
 * @date : 2021/4/1  11:19
 * @description ：
 */
public class BaseCommonMultiAdapter<T extends MultiItemEntity> extends BaseMultiItemQuickAdapter<T, BaseCommonViewHolder> {

    /**
     * 实体类中绑定的 variable 中的 name
     */
    private final int variableId;

    /**
     * map key = itemType 不同布局对应的 类型
     *     value = layout 不同类型对应的 布局
     */
    private final Map<Integer, Integer> typeItemMap;

    public BaseCommonMultiAdapter(Map<Integer, Integer> typeItemMap, int variableId) {
        this.typeItemMap = typeItemMap;
        this.variableId = variableId;
    }

    public BaseCommonMultiAdapter(Map<Integer, Integer> typeItemMap, List<T> list, int variableId) {
        super(list);
        this.typeItemMap = typeItemMap;
        this.variableId = variableId;
    }

    @NotNull
    @Override
    public BaseCommonViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),typeItemMap.get(viewType), parent, false);
        BaseCommonViewHolder baseCommonViewHolder = new BaseCommonViewHolder(dataBinding);
        //这里必须绑定 一下CLickListener 否则导致点击事件不起效果
        bindViewClickListener(baseCommonViewHolder, viewType);
        return baseCommonViewHolder;
    }

    @Override
    protected void convert(@NotNull BaseCommonViewHolder baseHolder, T entity) {
        baseHolder.binding.setVariable(variableId, entity);
        //防止数据闪烁
        baseHolder.binding.executePendingBindings();

    }
}
